package servlet.account.control;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
@WebServlet("/Logout")
public class logout extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("SESSIONID".equals(cookie.getName())) {
                    // 使Session ID失效
                    HttpSession session = req.getSession();

                    // 销毁session
                    session.invalidate();
                    // 删除浏览器中的Cookie
                    cookie.setMaxAge(0);
                    resp.addCookie(cookie);

                    break;
                }
            }
        }
        // 重定向到登录页面或其他页面
        resp.sendRedirect("Home");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}
